﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ChatServerApplication
{
    public partial class ChatServerApplication : Form
    {

        public static DelegateLogEntry dle;
        public static DelegateUserUpdate duu;
        public delegate void DelegateLogEntry(String value);
        public delegate void DelegateUserUpdate(ClientObject client);

        public ChatServerApplication()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dle = new DelegateLogEntry(addLogEntry);
            duu = new DelegateUserUpdate(updateUser);
            try
            {
                updateUser( File.ReadAllLines("user.txt"));
            }
            catch (Exception ex)
            {
                MessageBox.Show("No User Created");
            }
            backgroundWorker1.RunWorkerAsync();
        }

        private void updateUser(String[] text)
        {
            DataSet User_Table = new DataSet();
            User_Table.DataSetName = "User_Table";
            User_Table.ReadXml("user.xml");
            dataGridView1.DataSource = User_Table;
            dataGridView1.DataMember = "User";
            int test = dataGridView1.ColumnCount;
            dataGridView1.Update();


            // cell 1 = name, 2 = pw, 3 = ip, 0 = picture (warum 0? muss etwas mit dataset zu tun haben)
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++ )
            {
                String temp = dataGridView1.Rows[i].Cells[3].Value.ToString();
                if (String.IsNullOrEmpty(temp))
                    dataGridView1.Rows[i].Cells[0].Value = ResourcenDatei.rotesX;
                else
                    dataGridView1.Rows[i].Cells[0].Value = ResourcenDatei.GruenerHakenButton;
            }
            /*            

            // dataGridView1.Rows.Clear();
            if (text.Length > 1)
                dataGridView1.Rows.Add(text.Length - 1);
            
            int counter = 0;

            List<ClientObject> ls = new List<ClientObject>();

            foreach (String element in text)
            {
                String[] sep = new String[] {";"};
                String[] temp = element.Split(sep,StringSplitOptions.None);
                ls.Add(new ClientObject(temp[0], temp[1], String.Empty));

                dataGridView1.Rows[counter].Cells[0].Value = temp[0];
                dataGridView1.Rows[counter].Cells[1].Value = temp[1];
                dataGridView1.Rows[counter].Cells[3].Value = ResourcenDatei.rotesX;
                counter++;
            }
            */
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            dle.Invoke("Starting Server..." + Tools.NL);
            
            Server srv = new Server(11111, 0);
        }

        public void addLogEntry(String text)
        {
            DateTime dt = DateTime.Now;

            if (!InvokeRequired)
            {
                richTextBox1.AppendText(dt.Year + "/" + dt.Month + "/" + dt.Day + " " + dt.ToShortTimeString() + ": " + text);
            }
            else
            {
                Invoke(new DelegateLogEntry(addLogEntry), new object[] { text });
            }
        }

        public void updateUser(ClientObject client)
        {
            if (!InvokeRequired)
            {
                //TODO: hier user finden und updaten
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    String temp = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    if (temp.Equals(client.Username))
                    {
                        dataGridView1.Rows[i].Cells[3].Value = client.Ip;
                        dataGridView1.Rows[i].Cells[0].Value = client.Status;
                    }
                }
            }
            else
            {
                Invoke(new DelegateUserUpdate(updateUser), new object[] { client });
            }
        }
    }
}

